package com.simple.datax.db.service.impl;

import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.SimpleMessage;
import com.simple.datax.SimpleObject;
import com.simple.datax.api.SimpleException;
import com.simple.datax.service.DBBackupService;
import com.simple.datax.service.SimpleDBException;
import com.simple.datax.utils.SimpleConstants;

/**
 * @Title: DBBackupServiceImpl.java
 * @Package com.simple.datax.db.service.impl
 * @Description: 数据库备份操作
 * @author Paladin Chen
 * @date 2012-12-21 下午6:25:05
 * @version V1.0
 */
// @   Service("dbRecvBackupService")
public class DBBackupServiceImpl extends AbstractDBServiceImpl implements DBBackupService {
	private static final Logger logger = LoggerFactory.getLogger(DBBackupServiceImpl.class);
	/**
	 * 备份类型：1、原始备份；2、解析后备份；3、发送备份
	 */
	private int backupType = 1;

	/**
	 * 是否同时处理相关的状态表
	 */
	private boolean procStatus = false;

	@Transactional
	@Override
	public void process(SimpleObject src) throws SimpleException {
		if (src instanceof SimpleMessage) {
			logger.debug("Begin to DB backup....." + ((SimpleMessage) src).toLog());

			switch (backupType) {
			case 2:
				backupParseMessage((SimpleMessage) src);
				break;

			case 3:
				backupSentMessage((SimpleMessage) src);
				break;

			default:
				backupOriMessage((SimpleMessage) src);
				break;
			}
			logger.debug("Finish DB backup....." + ((SimpleMessage) src).toLog());
		} else {
			//logger.error("This function Only proc SimpleMessage!!");
			throw new SimpleDBException("The Parameter is NOT SimpleMessage!!");
		}
	}

	private void backupOriMessage(SimpleMessage im) {
		logger.debug("Begin to backupOriMessage。" + im.toLog());
		// 处理状态
		if (procStatus) {
			try {
				insertProcing(im);
			} catch (Exception e) {
				logger.error(im.toLog() + " Insert DataxProcing Error:", e);
			}
		}

		// 备份
		try {
			insertRecvRaw(im);
		} catch (Exception e) {
			logger.error(im.toLog() + " Insert DataxRecvRawMsg Error:", e);
		}
		logger.debug("Finish backupOriMessage." + im.toLog());
	}

	private void backupParseMessage(SimpleMessage im) {
		logger.debug("Begin to backupParseMessage." + im.toLog());
		// 处理状态
		if (procStatus) {
			try {
				updateProcing(im.getRecvRawMsgid(), SimpleConstants.STATUS_PARSE);
			} catch (Exception e) {
				logger.error(im.toLog() + " Update DataxProcing Error:", e);
			}
		}

		try {
			insertParseRaw(im);
		} catch (Exception e) {
			logger.error(im.toLog() + " Insert DataxParseRawMsg Error:", e);
		}
		logger.debug("Finish backupParseMessage." + im.toLog());
	}

	private void backupSentMessage(SimpleMessage im) {
		logger.debug("Begin to backupSentMessage." + im.toLog());
		if (procStatus) {
			try {
				procSent(im);
			} catch (Exception e) {
				logger.error(im.toLog() + " Proc Sent Status Error:", e);
			}
		}

		try {
			insertSentRaw(im);
		} catch (Exception e) {
			logger.error(im.toLog()+" Insert Sent Raw Error:", e);
		}
		logger.debug("Finish backupSentMessage." + im.toLog());
	}

	public int getBackupType() {
		return backupType;
	}

	public void setBackupType(int backupType) {
		this.backupType = backupType;
	}

	public boolean getProcStatus() {
		return procStatus;
	}

	public void setProcStatus(boolean procStatus) {
		this.procStatus = procStatus;
	}

}
